package am.bll.stub;

import java.util.ArrayList;

import am.bll.ItemEditor;
import am.bll.ItemReader;
import am.bll.ItemSorter;
import am.bll.ItemWriter;
import am.bll.entity.AccountBook;
import am.bll.entity.AccountItem;
import am.bll.entity.basic.PaymentWay;
import am.exception.ArgumentNullException;
import am.exception.ArgumentOutOfRangeException;
import am.exception.DatabaseException;
import am.exception.InvalidOperationException;

public class AccountItemManagementStub implements ItemReader, ItemSorter, ItemWriter, ItemEditor
{
    private ArrayList<AccountItem> items  = new ArrayList<AccountItem>(); // 所有账目
    private int                    idSeed = 0;                           // id自动递增种子

    public AccountItemManagementStub()
    {
        // 模拟创建各种账目
        for (int i = 0; i < 20; i++)
        {
            AccountItem item = new AccountItem();
            item.setId(++idSeed);
            item.setAmount(null);
            item.setName(null);
            item.setPayment(PaymentWay.Cash);
            item.setPicUrl(null);
            item.setRemark(null);
            item.setTarget(null);
            item.setTime(null);
            item.setTotal(null);
        }

        // 绑定账本
    }

    @Override
    public void editItem(AccountItem item) throws ArgumentNullException, InvalidOperationException, DatabaseException
    {
        boolean hasItem = false;
        for (AccountItem o : items)
        {
            if (o.getId() == item.getId())
            {
                hasItem = true;
                o.setAmount(item.getAmount());
                o.setId(item.getId());
                o.setName(item.getName());
                o.setPayment(item.getPayment());
                o.setPicUrl(item.getPicUrl());
                o.setRemark(item.getRemark());
                o.setTarget(item.getTarget());
                o.setTime(item.getTime());
                o.setTotal(item.getTotal());
                return;
            }
        }

        if (!hasItem)
        {
            throw new InvalidOperationException("不存在对应的账目, 可能是Id设置的不正确");
        }

    }

    @Override
    public void writeItem(AccountItem item, AccountBook book) throws ArgumentNullException
    {
        // TODO Auto-generated method stub
    }

    @Override
    public void sortItems(ArrayList<AccountItem> items, int mode)
    {
        // item.amount从小到大排序
        for (int i = 0; i < items.size() - 1; i++)
        {
            int p = i;
            for (int j = i + 1; j < items.size(); j++)
            {
                if (items.get(j).getTotal().compare(items.get(p).getTotal()) < 0)
                {
                    p = j;
                }
            }

            if (p != i)
            {
                // 交换两个数
            }
        }

    }

    @Override
    public AccountItem[] readItems(int start, int end, int mode, AccountBook book) throws ArgumentNullException,
            ArgumentOutOfRangeException, InvalidOperationException, DatabaseException
    {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void fillItem(AccountItem item) throws ArgumentNullException, DatabaseException
    {
        // TODO Auto-generated method stub

    }

}
